// pages/topicality/kaixueshijian/kaixueshijian.js
//获取应用实例
const app = getApp()
const util = require("../../../utils/util.js");
Page({

    /**
     * 页面的初始数据
     */
    data: {
        showNav: true, // 快捷导航是否展开
        tAppid: app.globalData.tAppid,
        textraData: app.globalData.textraData,
        showShare: true, // 显示分享按钮
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        this.setData({
            citycode: options.city ? options.city : (wx.getStorageSync("citycode") ? wx.getStorageSync("citycode") : 'sz'),
        })
        util.getUrl(app.globalData.url).then((res) => {
            // 获取城市公众号二维码
            util.cityConfig(this.data.citycode).then(res => {
                this.setData({
                    qrcodeimg: res.qrcode,
                })
            })
            util.cityList().then((res) => {
                let citydata = res.allcity.filter(v => v.citycode == this.data.citycode);
                this.setData({
                    city: Array.isArray(citydata) ? citydata[0].cityname : wx.getStorageSync("city")
                })
                wx.setNavigationBarTitle({
                    title: this.data.city + '开学时间最新消息_' + this.data.city + '幼儿园|小学|中学|大学开学时间'
                })
            })
            this.getSchoolData();
        })
    },

    getSchoolData() {
        let data = {
            action: "schooltime",
            citycode: this.data.citycode
        };
        util.request('zhuanti.php', data, ).then(res => {
            if (res.data.data.daofen) {
                res.data.data.daofen.content = res.data.data.daofen.content
                    .replace(/<p([\s\w"=\/\.:;]+)((?:(="[^"]+")))/ig, '<p')
                    .replace(/<p([\s\w"=\/\.:;]+)((?:(style="[^"]+")))/ig, '<p')
                    .replace(/<p([\s\w"=\/\.:;]+)((?:(class="[^"]+")))/ig, '<p')
                    .replace(/<p>/ig, '<p class="p_class">')
                    .replace(/<img([\s\w"-=\/\.:;]+)((?:(style="[^"]+")))/ig, '<img$1')
                    .replace(/<img/gi, '<img style="width:100%;height:auto"')
                    // .replace(/<span>/ig, '<p class="p_class">')
            }
            wx.setNavigationBarTitle({
                title: res.data.data ? res.data.data.title : "开学时间"
            })
            this.setData({
                schoolTime: res.data.data.school,
                daofen: res.data.data.daofen,
                title: res.data.data.title
            })
        })
    },

    // 查看详情
    navDetail(e) {
        let id = e.currentTarget.dataset.id;
        let type = e.currentTarget.dataset.type;
        wx.navigateTo({
            url: '/pages/article/detail/detail?id=' + id + '&type=' + type + '&city=' + this.data.citycode,
        })
    },

    /**
     * 页面滑动事件的处理函数
     */
    onPageScroll: function(e) {
        this.setData({
            showNav: false,
        })
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {
        return {
            title: this.data.title ? this.data.title : "开学时间",

        }
    },

    // 分享朋友圈
    onShareTimeline: function() {
        return {
            title: this.data.title ? this.data.title : "开学时间",
            imageUrl: "http://m.bj.bendibao.com/news/kaixueshijian/images/head.png?2020",
            query: { city: this.data.citycode },
        }
    },
})